package com.promfy.db.demo;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class DBService {

	private DBOpenHelper dbHelper;
	private SQLiteDatabase db;
	private ContentResolver resolver;
	public DBService(Context context) {
		dbHelper = new DBOpenHelper(context);
	}

	/**
	 * search task
	 */
	public List<Reminders> getAll()  throws Exception{
		db = dbHelper.getWritableDatabase();
		List<Reminders> scoreList = new ArrayList<Reminders>();
		String sql = "SELECT  _id, date, score FROM score ORDER BY _id DESC LIMIT 50";
		Cursor cursor = null;
		try {
			cursor = db.rawQuery(sql, null);
			while (cursor.moveToNext()) {
				Reminders score = new Reminders(cursor.getString(1),cursor.getString(2));
				scoreList.add(score);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return scoreList; 
	}

	/**
	 * insert data to db
	 * @param task
	 * @throws Exception
	 */
	public void insert(Reminders score) throws Exception{
		db = dbHelper.getWritableDatabase();
		try {
			ContentValues cv = new ContentValues();
			cv.put("date", score.getDate());
			cv.put("score", score.getScore());
			db.insert("score", null, cv);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
}
